// DlgHoldRequest.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgHoldRequest.h"
#include "DlgPreviewHTML.h"
#include "SendMessage.h"


// CDlgHoldRequest dialog

IMPLEMENT_DYNAMIC(CDlgHoldRequest, CDialog)

CDlgHoldRequest::CDlgHoldRequest(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgHoldRequest::IDD, pParent)
	, m_szInfo(_T(""))
	, m_bEmailUser(TRUE)
	, m_dateHold(COleDateTime::GetCurrentTime())
	, m_bSendStaff(FALSE)
{
	m_nItemID = 0;
	m_nUserID = 0;
	m_nLibraryID = 0;
	m_nReason = 0;
	m_bDatePicked = FALSE;
	m_pszaLibrary = NULL;
}

CDlgHoldRequest::~CDlgHoldRequest()
{
}

void CDlgHoldRequest::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_STATIC_INFO, m_szInfo);
	DDX_Control(pDX, IDC_LIST_PICK, m_ctrlReason);
	DDX_Check(pDX, IDC_CHECK_SEND_EMAIL, m_bEmailUser);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER_HOLD, m_dateHold);
	DDX_Check(pDX, IDC_CHECK_SEND_STAFF, m_bSendStaff);
	DDX_Control(pDX, IDC_COMBO_LIBRARY, m_ctrlLibrary);
}


BEGIN_MESSAGE_MAP(CDlgHoldRequest, CDialog)
	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_DATETIMEPICKER_HOLD, &CDlgHoldRequest::OnDtnDatetimechangeDatetimepickerHold)
	ON_LBN_SELCHANGE(IDC_LIST_PICK, &CDlgHoldRequest::OnLbnSelchangeListPick)
	ON_BN_CLICKED(IDC_BUTTON_DATES, &CDlgHoldRequest::OnBnClickedButtonDates)
END_MESSAGE_MAP()


// CDlgHoldRequest message handlers

BOOL CDlgHoldRequest::OnInitDialog()
{
	CDialog::OnInitDialog();

	ASSERT( m_nItemID != 0 );
	ASSERT( m_pszaLibrary != NULL );

	// get request
	REQUEST_SET Request;
	if( !appConfig.m_SQL.GetRequestSet( &Request, m_nItemID ))
	{
		AfxMessageBox( _T("ERROR getting request.") );
		EndDialog( IDCANCEL );
		return TRUE;
	}
	// set variables
	m_nUserID = Request.m_user_id;
	m_nLibraryID = Request.m_library_id;
	m_szDateIn = Request.m_date_in.Format( appConfig.m_General.szDateFormat );

	// set m_ctrlLibrary
	INT nCurSel = 0;
	INT nSel = 0;
	CString szTemp;
	for( INT nIndex = 0; nIndex < m_pszaLibrary->GetCount(); nIndex++ )
	{
		szTemp = m_pszaLibrary->GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlLibrary.AddString( szTemp );
			m_ctrlLibrary.SetItemData( nSel, nIndex );
			if( m_nLibraryID == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlLibrary.SetCurSel( nCurSel );

	// get hold desricptions
	CArray<STATUS_LOOKUP> aStatus;
	CStringArray szaStatus;

	appConfig.m_SQL.GetDescriptionStatuss( &aStatus );
	//m_ctrlStatus
	for( INT nIndex = 0; nIndex < aStatus.GetCount(); nIndex++ )
	{
		// check for hold
		if( aStatus[nIndex].nID >= STATUS_OFFSET_HOLD && aStatus[nIndex].nID <= STATUS_OFFSET_HOLD + 99 )
		{
			// format text
			szTemp.Format( _T("%.3d   %s"), aStatus[nIndex].nID, aStatus[nIndex].szExplanation );
			// add to array
			nSel = m_ctrlReason.AddString( szTemp );
			m_ctrlReason.SetItemData( nSel, nIndex );
		}
	}

	// set title
	CString szText;
	szText.Format( _T("Hold Request: %.8ld"), m_nItemID );
	SetWindowText( szText );

	// get user
	USER_SET User;
	appConfig.m_SQL.GetUserSet( &User, m_nUserID );
	// get variables
	m_szTo = User.m_email;
	m_szInfo.Format( _T("Name: %s\r\nCompany: %s\r\nDate In: %s"), User.m_name, User.m_company, m_szDateIn );

	// hide date file
	if( appConfig.m_General.szHoldDateFile.IsEmpty() )
		GetDlgItem( IDC_BUTTON_DATES )->ShowWindow( SW_HIDE );

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgHoldRequest::OnOK()
{
	// update data
	UpdateData();

	// check for empty date
	if( !m_bDatePicked )
	{
		AfxMessageBox( _T("Please pick expected date before continuing.") );
		return;
	}

	// get library id
	LONG nLibrary = -1;
	nLibrary = m_ctrlLibrary.GetItemData( m_ctrlLibrary.GetCurSel() );
	if( nLibrary < 0 )
		return;

	// no triage
	if( appConfig.m_Options.bDisableTriageProcess && nLibrary == 0 )
	{
		AfxMessageBox( _T("Please pick a Library other then Triage for hold before continuing.") );
		return;
	}

	// get reason id
	m_nReason = m_ctrlReason.GetItemData( m_ctrlReason.GetCurSel() );
	if( m_nReason < 0 )
	{
		AfxMessageBox( _T("Please select reason for hold.") );
		return;
	}

	// get status description
	STATUS_LOOKUP Status;
	appConfig.m_SQL.GetDescriptionStatus( &Status, m_nReason );

	// show popup
	if( !Status.szInformation.IsEmpty() )
	{
		if( AfxMessageBox( Status.szInformation, MB_YESNO ) == IDNO )
			return;
	}

	// get request
	REQUEST_SET Request;
	appConfig.m_SQL.GetRequestSet( &Request, m_nItemID );

	// send message
	if( m_bEmailUser )
	{
		// get library info
		LIBRARY_LOOKUP Library;
		appConfig.m_SQL.GetDescriptionLibrary( &Library, m_nLibraryID );
		m_szFrom = Library.szEmail;
		m_szName = Library.szName;
		m_szSubject = Library.szSubject;

		// read message
		if( !Status.szMessageFile.IsEmpty() )
			m_szMessage = appConfig.ReadFile( Status.szMessageFile );
		else if( Request.m_external_id > 0 && !Status.szMessageFileExt.IsEmpty() )
			m_szMessage = appConfig.ReadFile( Status.szMessageFileExt );
		else
			m_szMessage = appConfig.ReadFile( appConfig.m_Email.szHoldMessageFile );

		// set hold date.
		m_szMessage.Replace( _T("(DATE_HOLD)"), m_dateHold.Format( appConfig.m_General.szShortDateFormat ));

		// get external request user id
		if( Request.m_external_id > 0 )
		{
			USER_SET User;
			if( appConfig.m_SQL.GetExternalUserID( &User, m_szTo ))
			{
				CString szTemp;
				szTemp.Format( _T("%ld"), User.m_id );
				m_szMessage.Replace( _T("(USERID)"), szTemp );
				m_szMessage.Replace( _T("(USERPID)"), User.m_pwd );
			}
		}

		// format message
		appConfig.FormatMacros( &m_szMessage, m_nItemID, -1, m_nReason );
		// format subject macros
		appConfig.FormatMacros( &m_szSubject, m_nItemID, -1, m_nReason );

		// show preivew
		CDlgPreviewHTML dlgPreview;
		dlgPreview.m_bSend = TRUE;
		dlgPreview.m_szHTML = m_szMessage;
		dlgPreview.m_szSubject = m_szSubject;

		// show message
		if( dlgPreview.DoModal() != IDOK )
			return;

		// get new message
		m_szMessage = dlgPreview.m_szHTML;
		m_szSubject = dlgPreview.m_szSubject;

		// send message
		CSendMessage send;
		if( !send.SendEmail( m_szName, m_szFrom, m_szTo, m_szSubject, m_szMessage ))
			return;
	}

	// send hold notice
	if( m_bSendStaff )
	{
		CString szNotify;

		// get library info
		LIBRARY_LOOKUP Library;
		appConfig.m_SQL.GetDescriptionLibrary( &Library, m_nLibraryID );
		m_szFrom = Library.szEmail;
		m_szName = Library.szName;
		m_szSubject = Library.szSubject;

		// get description_status
		STATUS_LOOKUP Status;
		appConfig.m_SQL.GetDescriptionStatus( &Status, m_nReason );
		szNotify = Status.szNotify;

		// check for email address
		if( !szNotify.IsEmpty() )
		{
			// read message
			m_szMessage = appConfig.ReadFile( appConfig.m_Email.szHoldNoticeFile );
			// format message
			appConfig.FormatMacros( &m_szMessage, m_nItemID, -1, m_nReason );

			// send message
			CSendMessage send;
			if( !send.SendEmail( m_szName, m_szFrom, szNotify, m_szSubject, m_szMessage ))
				return;
		}
	}

	// set variables
	Request.m_date_hold = m_dateHold;
	Request.m_status_id = m_nReason;
	Request.m_library_id = nLibrary;
	Request.m_retain = 0;
	// update request
	appConfig.m_SQL.SetRequestSet( &Request );

	// close dialog
	CDialog::OnOK();
}

void CDlgHoldRequest::OnDtnDatetimechangeDatetimepickerHold(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);

	// set date picked
	m_bDatePicked = TRUE;

	// return 0;
	*pResult = 0;
}

void CDlgHoldRequest::OnLbnSelchangeListPick()
{
	// update data
	//UpdateData();
	// get reason id
	CString szTemp;
	m_ctrlReason.GetText( m_ctrlReason.GetCurSel(), szTemp );
	m_nReason = _tstol( szTemp.Left( szTemp.Find( _T(" ") )));

	// get description_status
	STATUS_LOOKUP Status;
	appConfig.m_SQL.GetDescriptionStatus( &Status, m_nReason );
	// set check
	if( !Status.szNotify.IsEmpty() )
	{
		m_bSendStaff = TRUE;
		// set text
		szTemp.Format( _T("Send staff email. (%s)"), Status.szNotify );
		SetDlgItemText( IDC_CHECK_SEND_STAFF, szTemp );
	}
	else
	{
		m_bSendStaff = FALSE;
		// set text
		szTemp = _T("Send staff email.");
		SetDlgItemText( IDC_CHECK_SEND_STAFF, szTemp );
	}

	// update data
	UpdateData( FALSE );
}

void CDlgHoldRequest::OnBnClickedButtonDates()
{
	// open file
	ShellExecute( NULL, _T("open"), appConfig.m_General.szHoldDateFile, NULL, NULL, SW_SHOWNORMAL );
}
